﻿HmJS.$Import('core.browser');
HmJS.$Import('base.class');

HmJS.register('base.AbstractObject', function ($) {

	return new Class({

		Implements: [Options, Events, Class.Binds],

		parameters: ['options'],

		options: {
			useLang: false
		},

		initialize: function () {
			//normalize arguments
			var numArgs = arguments.length,
					options = {},
					parameters = this.parameters,
					numParams,
					index;

			if (numArgs > 0) {
				if (numArgs === 1 && (Jx.type(arguments[0]) === 'object') && parameters.length === 1 && parameters[0] === 'options') {
					options = arguments[0];
				} else {
					numParams = parameters.length;
					if (numParams <= numArgs) {
						index = numParams;
					} else {
						index = numArgs;
					}
					for (var i = 0; i < index; i++) {
						if (parameters[i] === 'options') {
							Object.append(options, arguments[i]);
						} else {
							options[parameters[i]] = arguments[i];
						}
					}
				}
			}

			this.setOptions(options);

			if (this.options.useLang) {
				// TODO
				//MooTools.lang.addEvent('langChange', this.bound('changeText'));
			}

			this.fireEvent('preInit');
			this.init();
			this.fireEvent('postInit');
			this.fireEvent('prePluginInit');
			this.initPlugins();
			this.fireEvent('postPluginInit');
			this.fireEvent('initializeDone');
		},

		destroy: function () {
			this.fireEvent('preDestroy');
			this.cleanup();
			this.fireEvent('postDestroy');
		},

		cleanup: function () {
			if (this.options.useLang && this.isExistBound('changeText')) {
				// TODO
				//MooTools.lang.removeEvent('langChange', this.bound('changeText'));
			}
			this.bound = null;
		},

		init: $ns.base.abstractMethod,

		changeText: $ns.base.abstractMethod,

		generateId: function (prefix) {
			prefix = (prefix) ? prefix : 'hm-';
			var uid = $uid(this);
			delete this.uid;
			return prefix + uid;
		}

	});
});
